Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7-oesnuj #24

Merged
merged 5 commits into from
Jul 6, 2024
Merged

7-oesnuj #24

merged 5 commits into from
Jul 6, 2024

Conversation

oesnuj
Copy link
Member

@oesnuj oesnuj commented May 7, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ | queuestack

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๐Ÿ’ก๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ๊ฐ€ ๋„ˆ๋ฌด ์ฝ๊ณ  ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ต๊ฒŒ ์ƒ๊ฒผ์–ด์š”
์ฒ˜์Œ ๋ฌธ์ œ๋ดค์„๋•Œ ์ด๊ฒŒ ๋ญ”๋ง์ด์ง€ ์‹ถ์—ˆ๋Š”๋ฐ ์ฒœ์ฒœํžˆ ๊ณ„์† ์ฝ๋‹ค๋ณด๋‹ˆ๊นŒ ์ดํ•ด๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
ํ’€๊ธฐ ์ „์—๋Š” ์–ด๋ ค์›Œ๋ณด์ด๋Š”๋ฐ ๋ง‰์ƒ ํ’€๋ฉด ๋ณ„๊ฑฐ ์•„๋‹ˆ๋‹ˆ ์•„์ง ์•ˆํ’€์–ด๋ณด์…จ๋‹ค๋ฉด ํ•œ๋ฒˆ ์ฒœ์ฒœํžˆ ์ฝ์–ด๋ณด์…”์š”

์•ž๋ถ€๋ถ„ ์ž…๋ ฅ

๊ฐ ์ž๋ฃŒ๊ตฌ์กฐ(0 : queue, 1 : stack)์— ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด๋‘๋Š” ๊ณผ์ •
์ด๋ ‡๊ฒŒ ๊ฐœ๋ณ„์ ์ธ ํ์™€ ์Šคํƒ์„ ๋ชจ์•„๋‘” ์ „์ฒด๋ฅผ ์ด ๋ฌธ์ œ์—์„œ๋Š” queuestack์ด๋ผ๊ณ  ํ•œ๋‹ค.
์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๊ตฌ์„ฑ๋œ๋‹ค.
image

image

๋’ท๋ถ€๋ถ„ ์ž…๋ ฅ

์œ„์—์„œ ๋งŒ๋“  queuestack์— ์ฒซ ๋ถ€๋ถ„(์Šคํƒ or ํ)์— push ์‹œํ‚ฌ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
image

queuestack์— push๋ฅผ ์‹œํ‚ค๊ณ  pop์„ ๋ฐ”๋กœ ์‹œํ‚จ๋‹ค.
์Šคํƒ์ด๋ฉด push ํ•œ ๊ฐ’์ด ๋ฐ”๋กœ pop
ํ๋ผ๋ฉด ๊ธฐ์กด์— ๋“ค์–ด์žˆ๋˜ ๊ฐ’์ด pop
์ด๋ ‡๊ฒŒ pop ๋œ ๊ฐ’์ด ๋‹ค์Œ ๋ถ€๋ถ„(์Šคํƒ or ํ)์— push ๋œ๋‹ค.

์œ„ ๊ณผ์ •์ด ๋ฐ˜๋ณต๋˜๊ณ  ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— pop ๋œ ๊ฐ’์ด ๋ฐ”๋กœ queuestack์˜ ๋ฆฌํ„ด๊ฐ’์ด๋‹ค.

์ž…๋ ฅ์— ๋”ฐ๋ฅธ ์ด ๋งˆ์ง€๋ง‰ return๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ œ์˜ ๋‹ต.

๋ฐฑ์ค€ 1๋ฒˆ ์˜ˆ์‹œ) queuestack์— 2๋ฅผ ๋„ฃ์—ˆ์„ ๋•Œ์˜ ๋ฆฌํ„ด๊ฐ’

image

โœ ํ’€์ด๊ณผ์ •

์œ„ ํ๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ์ ์€ ์Šคํƒ์€ push๋œ ๊ฐ’์ด ๊ณง pop๋˜๋Š” ๊ฐ’์ด๋ฏ€๋กœ ์—†๋‹ค๊ณ  ์ณ๋„ ๋œ๋‹ค.
0110
1234
์—์„œ

00
14
๋งŒ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
์œ„ ์ƒํ™ฉ์—์„œ ๋‘๋ฒˆ์งธ ์ž…๋ ฅ์— ๋”ฐ๋ฅธ queuestack์˜ return ๊ฐ’์„ ์‚ดํŽด๋ณด๊ฒ ๋‹ค.
queuestack : 1 4 <- 2๋ฅผ pushํ•˜๋ฉด ์ตœ์ข…์ ์œผ๋กœ 4๊ฐ€ ๋‚˜์˜จ๋‹ค.
queuestack : 2 1 <- 4๋ฅผ pushํ•˜๋ฉด ์ตœ์ข…์ ์œผ๋กœ 1์ด ๋‚˜์˜จ๋‹ค.
queuestack : 4 2 <- 7์„ pushํ•˜๋ฉด ์ตœ์ข…์ ์œผ๋กœ 2๊ฐ€ ๋‚˜์˜จ๋‹ค.
์œ„ ๊ทœ์น™์„ ๋ณด๋ฉด ํ•˜๋‚˜๊ฐ€ ๋“ค์–ด๊ฐ€๋ฉด ๋งˆ์ง€๋ง‰ ์›์†Œ๊ฐ€ ๋ฐ€์–ด์ ธ์„œ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ตœ์ข…์ ์œผ๋กœ deque ๊ตฌ์กฐ์— ๋‘๋ฒˆ์งธ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ ๊ฐ’์„ push_front ์‹œํ‚ค๊ณ  ๊ทธ๋•Œ pop ๋˜๋Š” ๊ฐ’์ด ์ •๋‹ต์ด๋‹ค.
์ด๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋งŒํผ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋œ๋‹ค.

image

๐Ÿ“‘ ์†Œ์Šค์ฝ”๋“œ

#include <iostream>
#include <deque>
#include <vector>
using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(NULL);
    int n;
    cin >> n;
    vector <int> dataStructure(n); //์Šคํ…์ธ์ง€ ํ์ธ์ง€ ์ž…๋ ฅ๋ฐ›๊ธฐ
    for (auto& i : dataStructure){
        cin >> i;
    }

    deque <int> queuestack; //queuestack์„ deque ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๋งŒ๋“ ๋‹ค.
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        if (dataStructure.at(i) == 0) //ํ์ธ ๊ฒฝ์šฐ์—๋งŒ ์ฒ˜๋ฆฌ, ์Šคํƒ์€ ๋ฌด์กฐ๊ฑด ์ž…๋ ฅ๊ฐ’์ด ๋‚˜์˜ค๊ธฐ์— ์—†๋‹ค๊ณ  ๋ณด๋ฉด๋จ
            queuestack.push_back(x);
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        int x;
        cin >> x;
        queuestack.push_front(x);  //์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š” ๊ฐ’์„ ๋ฑ์— pushํ•˜๊ณ 
        cout << queuestack.back() << " "; //๊ทธ๋•Œ back์„ ์ •๋‹ต์œผ๋กœ ์ถœ๋ ฅ
        queuestack.pop_back(); //์ถœ๋ ฅ ์ดํ›„์— ๋ฑ์—์„œ pop์‹œํ‚จ๋‹ค.
    }
    return 0;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํ, ์Šคํƒ, ๋ฑ์€ ์ด์ œ ๋งŽ์ด ํ’€์–ด๋ณธ๊ฒƒ ๊ฐ™์œผ๋‹ˆ ๋‹ค์Œ ๋ฌธ์ œ๋Š” ํŠธ๋ฆฌ๊ตฌ์กฐ๋กœ ์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@pu2rile pu2rile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋‹จ์ˆœ ์ž๋ฃŒ๊ตฌ์กฐ ๋ฌธ์ œ์ธ ์ค„ ์•Œ์•˜๋Š”๋ฐ ์ƒ๊ฐ๋ณด๋‹ค ์ €๋„ ๋ฌธ์ œ ์ดํ•ด๊ฐ€ ๊ฝค ๊ฑธ๋ ธ์–ด์š”
๋ฌผ๋ก  ๋ฌธ์ œ์— ๋‚˜์˜จ ์ด๋ฆ„์ด ์ œ ์ด๋ฆ„์ด๋ผ ๋‹นํ™ฉ๋„... ์ œ๋ฒ•...^^ ํ•œ๊ฐ€๋กญ๊ฒŒ ๋†€๊ณ  ์žˆ๋Š” ๋„ํ˜„........^^ ใ…Ž
๊ทธ๋ฆผ ์ž๋ฃŒ๋„ ๊ฐ™์ด ์˜ฌ๋ ค ์ฃผ์…”์„œ ๋•๋ถ„์— ์‰ฝ๊ฒŒ ํ•ด์„ํ•˜๊ณ  ์ดํ•ดํ–ˆ์Šต๋‹ˆ๋‹ค ์ˆ˜๊ณ ํ•˜์…จ์–ด์š”!

Copy link
Collaborator

@suhyun113 suhyun113 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ทธ๋ฆผ์œผ๋กœ ์—„์ฒญ ์ž์„ธํžˆ ์„ค๋ช…ํ•ด์ฃผ์…จ์–ด์š”!!!
์Šคํƒ ์œ ํ˜•์„ ๋จผ๊ฐ€ ์ข‹์•„ํ•ด์„œ ๊ดœํžˆ ์ข‹์•˜๋„ค์š” ๊ทผ๋ฐ,, ์–ด๋ ค์›Œ๋ณด์—ฌ์„œ ์‚ด์ง ๋ฌด์„œ์› ๋‹ค๊ฐ€ ์ค€์„œ๋‹˜ ๋ฌธ์ œ ์„ค๋ช…๋ณด๊ณ  ๋ง‰์ƒ ํ’€๋ฉด ๋ณ„๊ฑฐ ์•„๋‹ˆ๋ผ๋Š” ๋ง์— ์—ด์‹ฌํžˆ ์ฝ์–ด๋ณด์•˜์ง€์š”๐Ÿค“

๋ฐฑ์ค€ ์„ค๋ช…๋งŒ ๋ดค์„ ๋•Œ๋Š” ๋Š๋‚Œ์€ ์•Œ๊ฒ ๋Š”๋ฐ, ์ •ํ™•ํžˆ ์ดํ•ดํ•˜๊ธฐ ์ซŒ ํž˜๋“ค์—ˆ์–ด์š”ใ…œใ…œ ๊ทผ๋ฐ, ์ค€์„œ๋‹˜ ๊ทธ๋ฆผ ๋ณด๋ฉด์„œ ์ฒœ์ฒœํžˆ ์ดํ•ดํ•ด๋ณด๋‹ˆ๊นŒ ๋˜๋”๋ผ๊ตฌ์š”๐Ÿ˜ฎ ์ง„์งœ ๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช… ๋„ˆ๋ฌด ์ž˜ํ•ด์ฃผ์…จ์–ด์š”!

๋ฌธ์ œ๋Š” ์ดํ•ดํ–ˆ๊ณ  ์ฝ”๋“œ๋ฅผ ๋ดค๋Š”๋ฐ, deque๊ฐ€ ๋ญ”์ง€ ๊ธฐ์–ต์ด ์•ˆ ๋‚˜์„œ ๋‹ค์‹œ ์ฐพ์•„๋ณด๊ฒŒ ๋˜์—ˆ์–ด์š”, ๋ถ„๋ช… ์ด์ „์— ํ์™€ ์Šคํƒ์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋ฑ๋„ ๊ณต๋ถ€ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ,,ใ… ใ…  ๋ฑ์€ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ๋„ฃ๊ณ  ๋บ„ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์˜€์–ด์š”! ๋•๋ถ„์— ๊ธฐ์–ต์ด ๋‚ฌ์Šต๋‹ˆ๋‹ค. ํ์ธ ๊ฒฝ์šฐ์—๋Š” ๊ธฐ์กด์˜ ๊ฐ’์„ popํ•ด์•ผ ํ•˜๊ณ , ์Šคํƒ์ธ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰์— ๋„ฃ์€ ๊ฐ’์„ popํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— depue๋ฅผ ์‚ฌ์šฉํ•œ ๊ฑฐ๋ผ๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์•˜๋Š”๋ฐ..๋Œ€๋‹จํ•œ ๊ฒƒ ๊ฐ™์•„์š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด ์ด๋Ÿฐ๊ฒŒ ๋ฐ”๋กœ ์ƒ๊ฐ์ด ๋‚˜๋‚˜์š”? ๋ถ„๋ช… ์•Œ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ deque๊ฐ€ ์ƒ๊ฐ๋‚˜์ง€ ์•Š์•˜๊ฑฐ๋“ ์š”,, ๋” ๊ณต๋ถ€ํ•ด์•ผ ๊ฒ ์Šต๋‹ˆ๋‹คโ—

์Šคํƒ์ธ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ๊ฐ’์ด pop๋˜๋ฏ€๋กœ ๊ธฐ์กด์˜ ๊ฐ’์ด ์ž…๋ ฅ๊ฐ’์ด ๋˜์–ด ์ฒ˜๋ฆฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ๋„ ์ดํ•ด๋Š” ํ–ˆ๋Š”๋ฐ, ์ฝ”๋“œ์—์„œ ์ œ์™ธ์‹œํ‚ฌ ์ƒ๊ฐ์€ ๋ชป ํ–ˆ๋Š”๋ฐ ํ›จ์”ฌ ํšจ์œจ์ ์ผ ๊ฒƒ ๊ฐ™์•„์š”!

back()์ด ๋ญ”์ง€ ๋ชฐ๋ผ์„œ ๋˜ ์ฐพ์•„๋ณด๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, ๋ฒกํ„ฐ๋ฅผ ๋ฐฐ์šฐ๋ฉด์„œ ๋ฐฐ์› ๋˜ ๋‚ด์šฉ์ด์—ˆ๋”๊ตฐ์š”.. ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์˜€๋„ค์š”! ๋Š๋‚Œ์€ ์•Œ๊ฒ ๋Š”๋ฐ ์ œ๋Œ€๋กœ ๊ธฐ์–ต๋‚˜์ง€ ์•Š๋Š” ๊ฒƒ๋“ค์ด ๋งŽ์€ ๊ฒƒ ๊ฐ™์•„์š”. ๋‹ค์‹œ ์ฐพ์•„๋ณด๊ฒŒ ๋˜๋ฉด์„œ ๋ฒกํ„ฐ์˜ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋“ค์„ ํ•œ ๋ฒˆ ๋” ๊ณต๋ถ€ํ•˜๊ฒŒ ๋˜์–ด ์ข‹์•˜์Šต๋‹ˆ๋‹ค

๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ๋‚˜์„œ๋„ ์ด๊ฑธ ๋‚ด๊ฐ€ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„๊นŒ ๊ฐ์ด ์ž˜ ์˜ค์ง€ ์•Š์•˜๋Š”๋ฐ,,, ์ค€์„œ๋‹˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ์ดํ•ดํ•ด๋ณด๋‹ˆ๊นŒ ์ƒ๊ฐ๋ณด๋‹ค ํ• ๋งŒ ํ–ˆ์–ด์š”! ์ œ ํž˜์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งœ๋Š” ๊ฑด ์‹คํŒจํ–ˆ์ง€๋งŒ ์ดํ•ดํ–ˆ์œผ๋‹ˆ ๋‹ค์Œ๋ฒˆ์—๋Š” ์ œ ํž˜์œผ๋กœ ์งœ๋ณด๋ ค๊ตฌ์š”!

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹คใ…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants